(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTime();
			initLeave(1,pageSize);
		});
	});	
	function initTime(){
		var date = new Date();
		var newDate = getFormatDate(date,"yyyy-MM-dd hh:mm");
		var startTime = getFormatDate(new Date(date.getTime()-1000*60*60*24),"yyyy-MM-dd ");
		$("#start-time").val(startTime+"00:00");
		$("#end-time").val(newDate.substring(0,11)+"23:59");
	}
	function initLeave(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $(".classes-choice-select",parent.document).attr("classcode");
		var flag = $("#tleave-flag option:selected").val();
		var starttime = strToDate($("#start-time").val() + ":00")/1000;
		var endtime = strToDate($("#end-time").val() + ":00")/1000;
		var obj = {
				starttime:starttime,
				endtime:endtime,
				schoolcode:schoolcode,
				currentpage:currentpage,
				pagesize:pagesize,
				flag:flag,
		};
		if(classcode != "" && classcode != undefined){
			obj["classcode"] = classcode;
		}
		var data = {
				cmd:"teacherGetLeave",
				model:"leave",
				userKey:userKey,
				data:obj,
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				var total = data[0].total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize <= 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#tleave-page").empty();
				$("#tleave-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initLeave(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initLeave((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize%5 == 0){
							if((now+1) < totalSize/5){
								now ++;
								initLeave((now*5+1),pageSize);
							}else{
								return false;
							}
						}else{
							if((now+1) <= (totalSize-totalSize%5)/5){
								now ++;
								initLeave((now*5+1),pageSize);
							}else{
								return false;
							}
						}
					}
				});
				if(data.length > 0){
					dataCache = [];
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
							if(key == "flag"){
								if(data[i][key] == 0){
									data[i][key] = "未审批";
								}else if(data[i][key] == 1){
									data[i][key] = "通过";
								}else if(data[i][key] == 2){
									data[i][key] = "驳回";
								}
							}
							if(data[i].agreetime == ""){
								agreetime = "暂无";
							}else{
								agreetime = getFormatDateByLong(parseInt(data[i].agreetime)*1000,"yyyy-MM-dd hh:mm");
							}
						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
								"<div class=\"table-name\" style='width:220px;'>"+ data[i].name + "</div>" +
								"<div class=\"table-code\" style='width:220px;'>请假时间： "+  getFormatDateByLong(parseInt(data[i].createtime)*1000,"yyyy-MM-dd hh:mm") +"</div>" +
							"</div>" +
							"<div class=\"tables-middle\" style='margin-left:25%;'>" +
								"<div class=\"table-class\" style='width:300px;'>"+ getFormatDateByLong(parseInt(data[i].starttime)*1000,"yyyy-MM-dd hh:mm") + " ~ " + getFormatDateByLong(parseInt(data[i].endtime)*1000,"yyyy-MM-dd hh:mm") +"</div>" +
								"<div class=\"table-code\" style='width:300px;'>审批时间："+ agreetime +"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\">审批状态："+ data[i].flag +"</div>" +
								"<div class=\"table-oprate\" style='margin-left:80px;'>" +
									"<div class=\"table-edit\" dataindex = \""+i+"\" studentcode = '"+data[i].studentcode+"' id = '"+data[i].id+"' flag = '"+data[i].flag+"'  title=\"审批\"></div>" +
								"</div>" +
							"</div>" +
						"</div>";
						dataCache.push(data[i]);
					}
					$("#tleave-data-show").empty();
					$("#tleave-data-show").append(content);
					
					$(".table-trs").unbind("hover");
					$(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$(".table-oprate").hide();
					});
					$(".table-edit").unbind("click");
					$(".table-edit").click(function(){
						$("#tleave-form-show").show();
						$("#right-tables").hide();
						var id = $(this).attr("id");
						var index = parseInt($(this).attr("dataindex"));
						showData(dataCache[index]);
						$("#tleave-form-show").contents().find("#back-to-table").unbind("click");
						$("#tleave-form-show").contents().find("#back-to-table").click(function(){
							$("#tleave-form-show").hide();
							$("#right-tables").show();
						});
						$("#tleave-form-show").contents().find("#tleave-agree").unbind("click");
						$("#tleave-form-show").contents().find("#tleave-agree").click(function(){
							var flag = "1";
							if(checkSave()){
								if(check2()){
									approveLeave(id,flag);
								}
							}
						});
						$("#tleave-form-show").contents().find("#tleave-disagree").unbind("click");
						$("#tleave-form-show").contents().find("#tleave-disagree").click(function(){
							var flag = "2";
							if(checkSave()){
								if(check2()){
									approveLeave(id,flag);
								}
							}
						});
						check1("tleaveform-reason1",isNull,"null","tleave-form-show");
					});
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#tleave-data-show").empty();
					$("#tleave-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#tleave-data-show").empty();
				$("#tleave-data-show").append(content);
			}
		},null,null);	
	}
	function approveLeave(id,flag){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var reason = $("#tleave-form-show").contents().find("#tleaveform-reason1").val();
		if(flag == "1"){
			obj = {
					flag:"1",
					id:id,
					remark:encodeURI(reason),
			};
		}else if(flag == "2"){
			obj = {
					flag:"2",
					id:id,
					remark:encodeURI(reason),
			};
		}
		var data = {
				cmd:"agreeLeave",
				model:"leave",
				userKey:userKey,
				data:obj,
				remark:"",					
		};
		processBegin("正在应用中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("审批成功");
				initLeave(1,pageSize);
			}else{
				processError("审批失败");
			}
		},null,null);
	}

	function showData(data){
		$("#tleave-form-show").contents().find("#tleaveform-name").val(data.name);	
		$("#tleave-form-show").contents().find("#tleaveform-type").val(data.type);	
		$("#tleave-form-show").contents().find("#tleaveform-starttime").val(getFormatDateByLong(parseInt(data.starttime)*1000,"yyyy-MM-dd hh:mm"));		
		$("#tleave-form-show").contents().find("#tleaveform-endtime").val(getFormatDateByLong(parseInt(data.endtime)*1000,"yyyy-MM-dd hh:mm"));					
		$("#tleave-form-show").contents().find("#tleaveform-reason").val(data.reason);	
		$("#tleave-form-show").contents().find("#tleaveform-reason1").val(data.remark);	
	}
	function check2(){
		var flg = false;
		var checkFails = $("#tleave-form-show").contents().find(".check-fail");
		for(var i = 0; i < checkFails.length; i++){
			if(checkFails[i].innerHTML != ""){
				flg = false;
				break;
			}else{
				flg = true;
			}
		}
		return flg;
	}
	function checkSave(){ 
		if($("#tleave-form-show").contents().find("#tleaveform-reason1").val() == ""){
			$("#tleave-form-show").contents().find("#tleaveform-reason1").focus();
			$("#tleave-form-show").contents().find("#tleaveform-reason1-ex").text("不能为空！");
			return false;
		}else{
			return true;
		}
};
	function pagebtns(){
		$("#tleave-search-btn").click(function(){
			now = 0;
			initLeave(1,pageSize);
		});
		$(".classes-choice",parent.document).unbind("click");
		$(".classes-choice",parent.document).click(function(){
			$(".classes-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("classes-choice-select");
			now = 0;
			initLeave(1,pageSize);
			
		});	
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();